
var xmlHttp;

/******************************************************************************
	variable to keep track if the page is changed
 *****************************************************************************/

var pageChanged = false;

/******************************************************************************
	variable to keep track no. of questions ansered
 *****************************************************************************/
var answeredCount=0;

/******************************************************************************

 *FunctionName          :isblank()
 *Description           :The function to check if the given string is blank
 *Input Parameters      :sring to be schecked for blank
 *Returns               :true if blank else false               

 *****************************************************************************/
function isblank(s){
	for(var i = 0; i < s.length; i++){
	var c = s.charAt(i);
	if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

/******************************************************************************

 *FunctionName          :verify()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/			
function verify(f){				
	var msg;
	
	var empty_fields = "";
	
	for(var i = 0; i < f.length; i++){
		var e = f.elements[i];
	
		if (((e.type == "text") || (e.type == "password")) && !e.optional){
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
	
				if(e.name == "uid"){
	    			empty_fields += "\n" + "User name";
				}else if(e.name == "strPassword"){
	    			empty_fields += "\n" + "Password";
				}
				
				continue;
			}
		}
	}
	
	if (!empty_fields){
	    loginSubmit(f);
	    
	 }
		
	
	
	if (empty_fields) {
	msg = "The following required field(s) are empty:"
	+ empty_fields + "\n";
	}
	if (empty_fields) {
		alert(msg);
		
			document.login.action="login.jsp"
		
				
		return false;
	}
	return true;
}
/******************************************************************************

 *FunctionName          :verify_1()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :chnage password form name
 *Returns               :none               

 *****************************************************************************/			
function verify_1(f){				
	var msg;
	
	var empty_fields = "";
	
	for(var i = 0; i < f.length; i++){
		var e = f.elements[i];
	
		if (((e.type == "text") || (e.type == "password")) && !e.optional){
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
	
				if(e.name == "uid"){
	    			empty_fields += "\n" + "User name";
				}else if(e.name == "strPassword"){
	    			empty_fields += "\n" + "Password";
				}else if(e.name == "newPassword"){
	    			empty_fields += "\n" + "NEW Password";
				}else if(e.name == "confirmPassword"){
	    			empty_fields += "\n" + " Confirm Password";
				}
				
				continue;
			}
		}
	}
	
	if (!empty_fields){
	    loginSubmit(f);
	    
	 }
		
	
	
	if (empty_fields) {
	msg = "The following required field(s) are empty:"
	+ empty_fields + "\n";
	}
	if (empty_fields) {
		alert(msg);
		
			document.login.action="changepassword.jsp"
		
				
		return false;
	}
	return true;
}
/******************************************************************************

 *FunctionName          :verify_1()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :add new user form name
 *Returns               :none               

 *****************************************************************************/			
function verify_2(f){				
	var msg;
	
	var empty_fields = "";
	var password = "";
	var uid="";
	for(var i = 0; i < f.length; i++){
		var e = f.elements[i];
	
		if (((e.type == "text") || (e.type == "password")) && !e.optional){
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
	
				if(e.name == "uid"){
	    			empty_fields += "\n" + "User name";
	    			uid=e.value;
				}else if(e.name == "Password"){
	    			empty_fields += "\n" + "Password";
	    			password=e.value;
				}else if(e.name == "confirmPassword"){
	    			empty_fields += "\n" + " Confirm Password";
				}
				
				
				
				
			}
		}
		
		if(e.name == "uid"){
			uid=e.value;
		}
		else if(e.name == "Password"){
			password=e.value;
		}
		
		
		if(e.name == "invalidusername" && e.value==uid)
		{
			empty_fields += "\n" + " User Id Already Present";
			
		}else if(e.name == "confirmPassword" && e.value != password){
			
			empty_fields += "\n" + " Password and Confirm Password Don't Match";
		}
		
		if(e.name == "usertype" && e.value == -1)
		{
			empty_fields += "\n" + " User Type";
		}
	}
	
	/*if(f.elements[3].value=="-1")
	{
		empty_fields += "\n" + " USER TYPE";
	}
	
	if(f.elements[1].value!=f.elements[2].value)
	{
		empty_fields += "\n" + "!! Password and confirm password dosent match!!!";
	}*/
	
	
	if (!empty_fields){
	    loginSubmit(f);
	    
	 }
	
		
	
	
	if (empty_fields) {
	msg = "The following required field(s) are empty:"
	+ empty_fields + "\n";
	}
	if (empty_fields) {
		alert(msg);
		
			document.login.action="addnewuser.jsp"
		
				
		return false;
	}
	return true;
}


/******************************************************************************

 *FunctionName          :verify_3()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/			
function verify_3(f){				
	var msg;
	
	var empty_fields = "";
	var sexchecked=false;
	
	for(var i = 0; i < f.length; i++){
		var e = f.elements[i];
	
		if (((e.type == "text") ) && !e.optional){
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
	
				if(e.name == "firstname"){
	    			empty_fields += "\n" + "First Name";
				}else if(e.name == "lastname"){
	    			empty_fields += "\n" + "Last Name";
				}
				else if(e.name == "address"){
	    			empty_fields += "\n" + "Address";
				}else if(e.name == "idcardtype"){
	    			empty_fields += "\n" + "ID Card Type";
				}else if(e.name == "idcardno"){
	    			empty_fields += "\n" + "Id Card Number";
				}else if(e.name == "medical"){
	    			empty_fields += "\n" + "Medical Problem";
				}
				
				continue;
			}
		}
		
		
		if(e.name == "profession" && e.value == -1)
		{
			empty_fields += "\n" + " Patient Profession";
		}
		else if(e.name == "sex" && e.checked==true)
		{
			sexchecked=true;
			f.elements["patientsex"].value=e.value;
			
		} 
		 
		
		
	}
	
	if(sexchecked==false)
	{
		empty_fields += "\n" + " SEX";
	}
		
	if (!empty_fields){
	    loginSubmit(f);
	    
	 }
		
	
	
	if (empty_fields) {
	msg = "The following required field(s) are empty:"
	+ empty_fields + "\n";
	}
	if (empty_fields) {
		alert(msg);
		
			//document.login.action="addnewpatient.jsp";
		
				
		return false;
	}
	return true;
}


/******************************************************************************

 *FunctionName          :verify_3()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/			
function verify_4(f){				
	var msg;
	
	var empty_fields = "";
	var sexchecked=false;
	
	for(var i = 0; i < f.length; i++){
		var e = f.elements[i];
	
		if (((e.type == "text") ) && !e.optional){
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
	
				if(e.name == "roomno"){
	    			empty_fields += "\n" + "Room Number";
				}else if(e.name == "charges"){
	    			empty_fields += "\n" + "Room Charges";
				}
				
				continue;
			}
		}
		
		 if(e.name == "dom" && e.checked==true)
		{
			sexchecked=true;
			f.elements["isdom"].value=e.value;
			
		} 
		
		
	}
	
	if(sexchecked==false)
	{
		empty_fields += "\n" + " IS Dom";
	}
		
	if (!empty_fields){
	    loginSubmit(f);
	    
	 }
		
	
	
	if (empty_fields) {
	msg = "The following required field(s) are empty:"
	+ empty_fields + "\n";
	}
	if (empty_fields) {
		alert(msg);
		
			document.login.action="addnewroom.jsp";
		
				
		return false;
	}
	return true;
}


/******************************************************************************

 *FunctionName          :verify_3()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/			
function verify_5(f){				
	var msg;
	
	var empty_fields = "";
	var sexchecked=false;
	
	for(var i = 0; i < f.length; i++){
		var e = f.elements[i];
	
		if (((e.type == "text") ) && !e.optional){
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
	
				if(e.name == "bedno"){
	    			empty_fields += "\n" + "Bed Number";
				}else if(e.name == "charges"){
	    			empty_fields += "\n" + "Room Charges";
				}
				
				continue;
			}
		}
		
	
	}
	
	if (!empty_fields){
	    loginSubmit(f);
	    
	 }
		
	
	
	if (empty_fields) {
	msg = "The following required field(s) are empty:"
	+ empty_fields + "\n";
	}
	if (empty_fields) {
		alert(msg);
		
			document.login.action="addnewbed.jsp";
		
				
		return false;
	}
	return true;
}


/******************************************************************************

 *FunctionName          :verifyWelcome()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/			
function verifyWelcome(f){	

	

	var msg;
	var empty_fields = "";
	
	for(var i = 0; i < f.length; i++){
	
		var e = f.elements[i];
	
		if (e.type == "select" && !e.optional){
	
			// first check if the field is empty
			if ((!e.value == null) || !(e.value == "") || !isblank(e.value)) {
	
				if(e.name == "userType"){
			
	    			empty_fields += "\n" + "User Type";
				}
				continue;
			}
		}
	}
	
	if (!empty_fields){

	    welcomeSubmit(f);
	 }
		
	
	
	if (empty_fields) {
	msg = "The following required field(s) are empty:"
	+ empty_fields + "\n";
	}
	if (empty_fields) {
		alert(msg);
		return false;
	}
}

/******************************************************************************

 *FunctionName          :verifyExamDetails()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/			
function verifyExamDetails(f){	

	
	    detailsSubmit(f);
	
}

/******************************************************************************

 *FunctionName          :submitAddQuestions()
 *Description           :the function to verify the values given in form are 
 						not empty
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/			
function submitAddQuestions(f){				
	var msg;
	var empty_fields = "";
	
	for(var i = 0; i < f.length; i++){
		var e = f.elements[i];
	
		if ((e.type == "text") && !e.optional){
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
	
				if(e.name == "examType"){
	    			empty_fields += "\n" + "Exam type";
				}else if(e.name == "question"){
	    			empty_fields += "\n" + "question";
				}else if(e.name == "answerOptions"){
	    			empty_fields += "\n" + "answerOptions";
				}else if(e.name == "correctAnswer"){
	    			empty_fields += "\n" + "correctAnswer";
				}
				continue;
			}
		}
	}
	
	if (!empty_fields){
	
	    addQuestionSubmit(f);
	 }
		
	
	
	if (empty_fields) {
	msg = "The following required field(s) are empty:"
	+ empty_fields + "\n";
	}
	if (empty_fields) {
		alert(msg);
		
		return false;
	}
}






/******************************************************************************

 *FunctionName          :loginSubmit()
 *Description           :The function submit the login form
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/				
function loginSubmit(formName){
	document.getElementById("event").value = "loginEvent";
    formName.submit();
}	
/******************************************************************************

 *FunctionName          :detailsSubmit()
 *Description           :The function submit the exam details form
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/				
function detailsSubmit(formName){
	
	document.getElementById("event").value = "examDetails";
    formName.submit();
}			
/******************************************************************************

 *FunctionName          :welcomeSubmit()
 *Description           :The function submit the welcome form
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/				
function welcomeSubmit(formName){
	
	document.getElementById("event").value = "welcomeEvent";
    formName.submit();
}

/******************************************************************************

 *FunctionName          :addQuestionSubmit()
 *Description           :The function submit the welcome form
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/				
function addQuestionSubmit(formName){
	document.getElementById("event").value = "questionEvent";
    formName.submit();
}

function editQuesAnswer(formName){

document.getElementById("event").value = "editQuesAnswerEvent";
							
formName.submit();		

}	
/******************************************************************************

 *FunctionName          :submitExamType()
 *Description           :The function to submit the exam type and open 
 						questions window
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/
function submitExamType(formName){
	if(formName.examtype.options[0].selected){
		alert("Please select an examination type")
		return;
	}
	else{	
		var selectedVal = formName.examtype.value;
		window.opener= "x";
		window.close();
		questionsWindow= window.open("/OnlineExamination/examServlet?event=fetchquestionsEvent&examtype="+selectedVal, "QuestionsWindow",
    		"location=1,status=1,scrollbars=1,toolbar=0,resizable=1,width=screen.availWidth,height=screen.availHeight,menubar=0,fullscreen=1");
		questionsWindow.moveTo(0,0);				
	}
}

/******************************************************************************

 *FunctionName          :submitViewReport()
 *Description           :The function to submit the exam type and open 
 						questions window
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/
function submitViewReport(formName){
	if(formName.examtype.options[0].selected){
		alert("Please select an examination type")
		return;
	}
	else{
	
		var selectedVal = formName.examtype.value;
		
		document.getElementById("event").value = "reportGenerateEvent";
   		 formName.submit();				
					
	}
}


/******************************************************************************

 *FunctionName          :submitViewReport()
 *Description           :The function to submit the exam type and open 
 						questions window
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/
function submitUser(formName){
	if(formName.selectUser.options[0].selected){
		alert("Please Select a User")
		return;
	}
	else{
	
		var selectedVal = formName.selectUser.value;
		
		
		document.getElementById("event").value = "selectUserEvent";
   		 formName.submit();				
					
	}
}

/******************************************************************************

 *FunctionName          :submitUserExam()
 *Description           :The function to submit the exam type and open 
 						questions window
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/
function submitUserExam(formName){
	if(formName.selectUser.options[0].selected){
		alert("Please select a User")

	}else if(formName.examtype.options[0].selected){
	alert("Please select examId")
	return;
	}
	else{

		var selectedVal = formName.selectUser.value;
		var selectVal = formName.examtype.value;
		
		document.getElementById("event").value = "selectUserExamEvent";
   		 formName.submit();				
	}
}

/******************************************************************************

 *FunctionName          :submitUserExam()
 *Description           :The function to submit the exam type and open 
 						questions window
 *Input Parameters      :form name
 *Returns               :none               

 *****************************************************************************/
function deleteEdit(formName){


	if(formName.examtype.options[0].selected){
		alert("Please select Exam for Edit")
		return;
	}
	else{
	
			
		document.getElementById("event").value = "editDeleteEvent";
   		 formName.submit();				
	}
}

function selectExamForDeletion(formName){


	if(formName.examtype.options[0].selected){
		alert("Please Select Exam for Deactivation")
		return;
	}
	else{
	
			
		document.getElementById("event").value = "QuesDeleteEvent";
   		 formName.submit();				
	}
}



function selectExamForActive(formName){


	if(formName.examtype.options[0].selected){
		alert("Please select Exam for Activation")
		return;
	}
	else{
	
			
		document.getElementById("event").value = "QuesActiveEvent";
   		 formName.submit();				
	}
}

function deletQues(formName){

var radioOptns = document.getElementsByName("radioBut");

			
		
		 	for(var i=0;i<radioOptns.length;i++)	{
		 	
					
						if(radioOptns[i].checked == true)
						{
							
							document.getElementById("event").value = "deleteQuesEvent";
							
							formName.submit();					
						}
		
}
}

function ActivateQues(formName){

	var radioOptns = document.getElementsByName("radioBut");

			
		
		 	for(var i=0;i<radioOptns.length;i++)	{
		 	
					
						if(radioOptns[i].checked == true)
						{
							
							document.getElementById("event").value = "activeQuesEvent";
							
							formName.submit();					
						}
		
		}
}

function deleteAnswer(formName){
var count = 0;
var inputElems = document.getElementsByTagName("input");

	for(var i=0;i<inputElems.length;i++) {
	
						if(inputElems[i].type == "checkbox" ) {
						
						if(inputElems[i].checked == true){
							count = count+1;
						}
						
						}
					}


if(count == 0){

alert("Please select some answers to be deleted")
		return false;
		
}else {
document.getElementById("event").value = "deleteEditQuesEvent";
							
							formName.submit();		
}

}

function editAQuestion(formName){

var radioOptns = document.getElementsByName("radioBut");

		
		
		 	for(var i=0;i<radioOptns.length;i++)	{
		 	
					
						if(radioOptns[i].checked == true)
						{
							
							document.getElementById("event").value = "editQuesEvent";
						
							formName.submit();					
						}
					}


}


/****************************************************************************


*****************************************************************************/
/****************************************************************************


*****************************************************************************/
function userExamMap(f){
var selectType = document.getElementById('examType').value;


var count = 0;
var inputElems = document.getElementsByTagName("input");

	for(var i=0;i<inputElems.length;i++) {
	
	
						if(inputElems[i].type == "checkbox" ) {
						
						if(inputElems[i].checked == true){
							count = count+1;
						}
						
						}
					}


if(count == 0){

alert("Please select some users to be mapped")
		return false;
		
}else if(selectType == -1)	{
		alert("please select exam type");
		document.getElementById('examType').focus;
		return false;
		}
else{
	document.getElementById("event").value = "userMapEvent";
	
    frmUserExam.submit();
    }
}
/******************************************************************************

 *FunctionName          :autoSubmit()
 *Description           :The function to auto submit the form after 
 						specified duration
 *Input Parameters      :none
 *Returns               :none               

 *****************************************************************************/
function autoSubmit(){
	alert("Exam duration is finished. Form will be autosubmitted");
	//if(document.getElementById("timeOut").Value="true";
	document.questions.action="/OnlineExamination/examServlet";
	//alert(document.questions2.action);
	document.getElementById("currTime").value=gCurrTime;
	document.getElementById("event").value = "calculateResultEvent";
	document.questions.submit();
	//alert("Exam duration is finished. Form will be autosubmitted");

	//document.form[0].submit();
	displayTime(0); 
	clearTimeout(time);
	
	//fnValidate1(true);
}

/******************************************************************************

 *FunctionName          :returnHome()
 *Description           :The function to return to login page frmo home button
 						click
 *Input Parameters      :none
 *Returns               :none               

 *****************************************************************************/
function returnHome(){
	var obj=document.getElementById("examtype");
	if(obj.value != -1) {
		if(confirm("The choice you have made will be lost..."))
			document.location.href="jsp/Login.jsp";
		else
			return;
	}
	else
		document.location.href="jsp/Login.jsp";
}

/******************************************************************************

 *FunctionName          :examTypeSelected()
 *Description           :The function to check any of the exam type is selected
 *Input Parameters      :none
 *Returns               :none               

 *****************************************************************************/
function examTypeSelected(){
	
		pageChanged = true;
}


function GetXmlHttpObject()
{

	var xmlHttp=null;

	try
	{
		
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

	}

return xmlHttp;

}

function stateChanged()
{
	if (xmlHttp.readyState==4)
	{
		document.getElementById("copy").innerHTML=xmlHttp.responseText;

	}

}

function useridcheck(str)
{
//alert("Button Pressed");

	if (str.length==0)
	{
		//alert("length is zero");
		document.getElementById("copy").innerHTML="";
		return;
	}

	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="checkusernames.jsp";
	url=url+"?q="+str;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function searchpatient(str)
{
//alert("Button Pressed");

	/*if (str.length==0)
	{
		//alert("length is zero");
		document.getElementById("copy").innerHTML="";
		return;
	}*/

	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="searchpatient.jsp";
	url=url+"?q="+str;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}
